﻿using AK56.Domain.Models;
using AK56.Domain.Services;
using AKCY.Tools;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Filters;

namespace AK56.APP.Application.Home.Controllers
{
    public class HomeController : ApiController
    {
        private ISYS_UsersService usersService;
        private ISYS_PagesService pagesService;

        public HomeController(ISYS_UsersService usersService,
            ISYS_PagesService pagesService)
        {
            this.usersService = usersService;
            this.pagesService = pagesService;
        }

        [HttpPost]
        public HttpResponseMessage Login(string username, string password)
        {
            
            if (string.IsNullOrEmpty(username))
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "请输入用户名！");
            }
            if (string.IsNullOrEmpty(password))
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "请输入密码！");
            }
            password = AKCY.Tools.EncryptEx.Encrypt.Md5(password);
            SYS_Users user = null;
            try
            {
                user = usersService.GetEntity(t => t.U_LoginName == username && t.U_Password == password);
            }
            catch (global::System.Exception)
            {

                throw;
            }
            if (user == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "用户名或密码不正确！");
            }
            if (user.U_Valid == (int)AK56.Domain.PubilcEnums.DataValid.Disabled)
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "您的账户已被禁用！");
            }
            user.U_SessionId = AKCY.Tools.GuidEx.GuidHelper.NewGuid().ToString();
            user.U_LastLoginTime = DateTime.Now;
            user.U_LastLoginIp = ((dynamic)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
            usersService.Update(user);

            return Request.CreateResponse(HttpStatusCode.OK, user.U_SessionId);
        }

        [HttpPost]
        public HttpResponseMessage CheckVersion(string versionCurrent)
        {
            string version = ConfigurationManager.AppSettings["Version"];
            bool result = version.Equals(versionCurrent);
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
        [HttpPost]
        public HttpResponseMessage CheckLogin(string sessionid)
        {
            if (string.IsNullOrEmpty(sessionid))
            {
                return Request.CreateResponse(HttpStatusCode.OK, new { status = false });
            }
            if (AK56.Domain.Untils.WebToolkit.IsValidSession(sessionid))
            {
                return Request.CreateResponse(HttpStatusCode.OK, new { status = false });
            }
            var user = usersService.GetEntity(t => t.U_SessionId == sessionid && t.U_Valid == (int)AK56.Domain.PubilcEnums.DataValid.Enabled);
            if (user != null)
            {
                string nodeName = user.U_Node_ID != null ? AK56.Domain.Untils.WebToolkit.GetDictionaryValue(user.U_Node_ID.ToString(), "Nodes", "") : "";
                string brancheName = AK56.Domain.Untils.WebToolkit.GetDictionaryValue(user.U_Branch_ID.ToString(), "Branches", "");
                return Request.CreateResponse(HttpStatusCode.OK, new
                {
                    status = true,
                    name = user.U_Name,
                    node = user.U_Node_ID,
                    branche = user.U_Branch_ID,
                    nodeName = nodeName,
                    brancheName = brancheName
                });
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.OK, new { status = false });
            }
        }

        [HttpPost]
        public HttpResponseMessage Logout(string sessionid)
        {
            if (string.IsNullOrEmpty(sessionid))
            {
                var user = usersService.GetEntity(t => t.U_SessionId == sessionid);
                if (user != null)
                {
                    user.U_SessionId = "";
                    usersService.Update(user);
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [HttpPost]
        public HttpResponseMessage Menu(string sessionid)
        {
            if (string.IsNullOrEmpty(sessionid))
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "菜单加载失败，请联系管理员！");
            }
            var data = pagesService.GetMenu(sessionid);
       //     var a = data.ToList();
            return Request.CreateResponse(HttpStatusCode.OK, data);
        }

        [HttpPost]
        public HttpResponseMessage Actions(string sessionid)
        {
            if (string.IsNullOrEmpty(sessionid))
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "权限加载失败，请联系管理员！");
            }
            var data = pagesService.GetActions(sessionid);
            return Request.CreateResponse(HttpStatusCode.OK, data);
        }


    }
}
